<?php
namespace App\Http\Controllers;

use App\Models\Agent;
use Illuminate\Http\Request;
use App\Models\city;
use App\Libraries\helpers;

class agent_admin extends Controller
{
    public function agent_admin(Request $request)
    {
        $search = $request -> get('search');
        $AgentSearch = isset($search)?$search:'';
        $m = 1;
        $page=$request->get('page');
        $page = isset($page)?$page:1;
        $n = ($page-1) * $m;
        $Agent =new Agent();
        $AgentRows = $Agent -> where('agent_name','like','%'.$AgentSearch.'%') -> skip($n) -> take($m) -> get();
        $AgentCount = $Agent -> where('agent_name','like','%'.$AgentSearch.'%') -> count('agent_id');
        return view('agent_admin',['AgentRows' => $AgentRows,'AgentCount' => $AgentCount,'AgentSearch' => $AgentSearch,'page'=>$page,'m'=>$m]);
    }
    public function agentadd_admin()
    {
        $city = new city();
        $cityRows = $city -> with('allChildren')->get();
        return view('agentadd_admin',['cityRows' => $cityRows]);
    }

    public function agent_add(Request $request)
    {
        $file = $request->file('agent_picture');//接收图片
        $demand = array('png','jpg');//后缀要求
        if(in_array($file->getClientOriginalExtension(),$demand)){
            $path = 'img';//文件名
            $dateName = date('Ymd');//时间
            $suffix = $file->getClientOriginalExtension();//获取后缀
            $fileName = md5(time().rand(1,100)).'.'.$suffix;//使用时间加随机数然后加密作为文件名
            $file->move($path,$fileName);
        }
        $input = $request -> all();
        $input['agent_picture'] = '/img/'.$fileName;
        $input['agent_city'] = $input['cat2'];
        unset($input['cat2']);
        unset($input['_token']);
        $Agent =new Agent();
        $agentAddRes = $Agent -> insert($input);
         return redirect('/agent_admin');
    }

    public function agent_detele(Request $request)
    {
        $Agent =new Agent();
        $agent_id = $request -> post('id');
        $agent_detele = $Agent -> where('agent_id',$agent_id) -> delete();
        if($agent_detele){return redirect('/agent_admin');}
    }

    public function agentedit_admin(Request $request)
    {
        $city = new city();
        $Agent =new Agent();
        $agent_id = $request -> post('id');
        $agentUpRows = $Agent -> where('agent_id',$agent_id) -> get();
        $cityRows = $city -> with('allChildren')->get();
        return view('agentupdate_admin',['cityRows' => $cityRows,'agentUpRows' => $agentUpRows]);
    }

    public function agentUpdate_admin(Request $request)
    {
        $file = $request->file('agent_picture');//接收图片
        $demand = array('png','jpg');//后缀要求
        if(in_array($file->getClientOriginalExtension(),$demand)){
            $path = 'img';//文件名
            $dateName = date('Ymd');//时间
            $suffix = $file->getClientOriginalExtension();//获取后缀
            $fileName = md5(time().rand(1,100)).'.'.$suffix;//使用时间加随机数然后加密作为文件名
            $file->move($path,$fileName);
        }
        $input = $request -> all();
        $input['agent_picture'] = '/img/'.$fileName;
        $input['agent_city']=$input['cat2'];
        unset($input['cat2']);
        unset($input['_token']);
        $Agent =new Agent();
        $agentUpRes = $Agent -> where('agent_id',$input['agent_id']) -> update($input);
//        var_dump($agentUpRes);die;
        return redirect('/agent_admin');
    }

}
